package me.nujoug.foctapp;

import me.nujoug.net.dao.DateStatus;
import me.nujoug.net.dao.DoctorInfo;
import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Adapter to bind a ToDoItem List to a view
 */
public class DoctItemAdapter extends ArrayAdapter<DoctorInfo> {

	/**
	 * Adapter context
	 */
	Context mContext;

	/**
	 * Adapter View layout
	 */
	int mLayoutResourceId;

	public DoctItemAdapter(Context context, int layoutResourceId) {
		super(context, layoutResourceId);

		mContext = context;
		mLayoutResourceId = layoutResourceId;
	}

	/**
	 * Returns the view for a specific item on the list
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;

		final DoctorInfo item = getItem(position);

		if (row == null) {
			LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
			row = inflater.inflate(R.layout.row_list_doct, parent, false);
		}

		row.setTag(item);
		final TextView itemView = (TextView) row.findViewById(R.id.DoctItem);
		itemView.setText(item.getNoonInDay() + "-"  + item.getDoctName() + "-" 
				+ item.getPrice() + "-" + item.getAvaiableTiacket() + "/"
				+ item.getTotalTicket() + "-" + item.getExpertise());

		final Button buttom = (Button) row.findViewById(R.id.buttonAppCatch);
		if (item.getAppUrl() != null && item.getAppUrl().startsWith("http")) {
			buttom.setText(DateStatus.APPOINT);
		} else {
			buttom.setText(DateStatus.FULL);
		}
		buttom.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(item.getAppUrl() == null || !item.getAppUrl().startsWith("http")){
					Toast toast = Toast.makeText(mContext.getApplicationContext(),
							item.getDoctName() + "-" + item.getDateStr() + ":" + DateStatus.FULL,
							Toast.LENGTH_LONG);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
					return;
				}

				// DateListActivity.this.prepareReferrel(bundle);
				((DoctListActivity) mContext).moveToNextActivity(item);
			}
		});
		return row;
	}
}
